//1026.节点与其祖先之间的最大差值
//https://leetcode.cn/problems/maximum-difference-between-node-and-ancestor/?envType=daily-question&envId=2024-04-05
class Solution {
public:
    int maxAncestorDiff(TreeNode* root) {
        return maxAncestorDiff(root,root->val,root->val);
    }
    int maxAncestorDiff(TreeNode* root,int mi,int ma) {
        if(!root) return 0;
        //检查当前节点与祖节点最大和最小值差值
        int ret = max(abs(root->val-mi),abs(root->val-ma));
        //更新最大最小值
        mi = min(root->val,mi);
        ma = max(root->val,ma);
        //继续遍历
        ret = max(ret,maxAncestorDiff(root->left,mi,ma));
        ret = max(ret,maxAncestorDiff(root->right,mi,ma));
        return ret;
    }
};